import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
import { ToastController } from '@ionic/angular';
import {environment} from '../../environments/environment';
import { EncryptService } from '../services/encrypt.service';
import { AuthTokenService } from '../services/auth-token.service';
import { stringify } from 'querystring';
import { TranslateService } from '@ngx-translate/core';
import { DefaultLanguageService } from '../services/default-language.service';
import { CustomToastProviderService,CustomToastOption } from '@common-components/common-toast/custom-toast-provider.service';
import { CommonMethodService } from './common.method.service';

export interface CommonResponse {
  isSuccess:boolean;
  errorCode:string;
  errorMsg:string;
  responseBody:any;
}

export enum ToastColor{
  success="success",
  warning="warning",
  dark="dark"
}

/**
 * SMS code type
 */
export enum VerificationType{
  LOGIN_SMS="LOGIN_SMS",
  LOGIN_APPUSERID = "LOGIN_APPUSERID",
	SIGNUP="SIGNUP",
  UPDATE_USER_PASSWORD_AFTER_LOGIN="UPDATE_USER_PASSWORD_AFTER_LOGIN",
  UPDATE_USER_PASSWORD_BEFORE_LOGIN="UPDATE_USER_PASSWORD_BEFORE_LOGIN",
	UPDATE_USER_MOBILENUMBER="UPDATE_USER_MOBILENUMBER",
  UPDATE_USER_EMAIL="UPDATE_USER_EMAIL",
  RESET_PAYMENT_PASSWORD="RESET_PAYMENT_PASSWORD",
  BANK_ACCOUNT="BANK_ACCOUNT"
}

export enum AutoLoginType {
	FINGER_PRINT="FINGER_PRINT",
  FACE_ID="FACE_ID",
}

export enum UpdateUserPasswordTypeEnum {
	WITH_OLD_PASSWORD = "WITH_OLD_PASSWORD",
	WITH_SMS_CODE = "WITH_SMS_CODE"
}

export enum AppParam{
  SMS_COUNT_DOWN = "SMS_COUNT_DOWN"
}

export interface AppVerificationCode {
  appUserId:string;
  verificationCode:string;
  verificationType:VerificationType;
  mobileNumber:string;
  appCode:string;
}

@Injectable({
  providedIn: 'root'
})

export class CommonService {

  constructor(private route: ActivatedRoute,
    private http:HttpClient,
    private toastController: ToastController,
    private encryptService:EncryptService,
    private authTokenService:AuthTokenService,
    private defaultLanguageService:DefaultLanguageService,
    private translate:TranslateService,
    private customToastProviderService:CustomToastProviderService,
    private commonMethodService:CommonMethodService) { }

  getAppUser(appUserIdOrMobileNumber){
    var api = "/scm/app/getAppUser.do";
    var requestJson = {
      'appUserIdOrMobileNumber':appUserIdOrMobileNumber
    }
    return this.http.post(api,requestJson);
  }

  sendAppVerificationCode(sendAppVerificationCode:AppVerificationCode){
    var api = "/scm/app/sendAppVerificationCode.do";
    return this.http.post(api,sendAppVerificationCode);
  }

  checkAppVerificationCode(checkAppVerificationCode:AppVerificationCode){
    var api = "/scm/app/checkAppVerificationCode.do";
    return this.http.post(api,checkAppVerificationCode);
  }

  checkPassword(password){
    var api = "/scm/app/checkPassword.do";
    var requestJson = {
      'appUserId':this.authTokenService.getAppUserId(),
      'password':this.encryptService.md5Ecrypt(password)
    }
    return this.http.post(api,requestJson);
  }

  commonToast(color:ToastColor,message?:string,duration?:number){
    if(!duration){
      duration = 2000;
    }
    if(color == ToastColor.warning && !message){
      let defaultLanguage = this.defaultLanguageService.getDefaultLanguage();
      if(defaultLanguage == 'zh_CN'){
        message = '操作失败';
      }else{
        message = 'operation failed';
      }
    }
    let iconShow = false;
    let iconSuccess = false;
    if(ToastColor.success == color){
      iconShow = true;
      iconSuccess = true;
    }
    let customToastOption:CustomToastOption = {
      icon:iconShow,
      iconSuccess:iconSuccess,
      message:message,
      duration:duration
    }
    this.customToastProviderService.show(customToastOption);
  }

  errorRespCommonToast(httpErrorResp:HttpErrorResponse,i18n?:Object,duration?:number){ 
    this.commonMethodService.errorRespCommonToast(httpErrorResp,false,i18n,duration);
  }

  initTranslate(modules:Array<string>){
    let defultLanguage = this.defaultLanguageService.getDefaultLanguage();
    this.translate.setDefaultLang(defultLanguage);
    this.translate.use(defultLanguage);
    return this.commonMethodService.geti18n(modules);
  }

  /**
   * 
   * @param hideMobileNumber 131****1234
   */
  getMobileNumber(hideMobileNumber){
    var api = "/scm/app/getMobileNumber.do";
    var requestJson = {
      'appUserId':this.authTokenService.getAppUserId(),
      'mobileNumber':hideMobileNumber
    }
    return this.http.post(api,requestJson);
  }

  getAppParamFromServer(appParam:AppParam){
    var api = "/scm/app/getAppParam.do";
    var requestJson = {
      'paramName':appParam
    }
    return this.http.post(api,requestJson);
  }

  cacheAppParam(appParam:AppParam,value:string){
    localStorage.setItem(appParam,value);
  }

  getAppParamFromCache(appParam:AppParam){
    return localStorage.getItem(appParam);
  }

}
